/**
  *Copyright(C),2020 - 2021, Company Tech. Co., Ltd.
  *FileName:   serial_echo.c
  *Date:       2025-02-15 13:44:20
  *Author:     maguoqiang
  *Version:    1.0
  *Path:       D:\project_document\serial_transmission\project\applications
  *Description:
*/

/* include ------------------------------------------------------------------*/
#include <rtthread.h>
#include "serial_trans.h"
#include "serial_echo.h"
/* private define -----------------------------------------------------------*/
#define COMPUTER_UART_NAME  "uart1"


struct rt_completion recv_ok;
static rt_err_t RecvDataSuccessHandler(rt_device_t dev, rt_size_t size);
/* private variables --------------------------------------------------------*/
static rt_device_t  computer_uart;
static rt_uint8_t serial_recv_buffer[BSP_UART1_RX_BUFSIZE];

/* public function prototype ------------------------------------------------*/

void echoInit(void)
{
    computer_uart =  rt_device_find(COMPUTER_UART_NAME);
}
void echoRecvTaskEntry(void *parameter)
{
	echoInit();
    serialInit(computer_uart);
    setUartRXHandler(computer_uart,RecvDataSuccessHandler);

    while(1)
    {
        serialReceive(computer_uart,serial_recv_buffer,BSP_UART1_RX_BUFSIZE);
        rt_thread_mdelay(100);
    }
}

void echoSendTaskEntry(void *parameter)
{
    while(1)
    {
        rt_completion_wait(&recv_ok,RT_WAITING_FOREVER);
        serialSend(computer_uart,serial_recv_buffer,BSP_UART1_RX_BUFSIZE);
        rt_thread_mdelay(100);
    }

}


/* private function prototype -----------------------------------------------*/


static rt_err_t RecvDataSuccessHandler(rt_device_t dev, rt_size_t size)
{
    //serialSend(dev,serial_recv_buffer,size);
    //释放完成量
    rt_completion_done(&recv_ok);
}

